package com.mjh.zhujianju.controller.party;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedResult;
import com.livew.framework.pojo.AjaxResult;
import com.mjh.zhujianju.model.party.BuildingExamEO;
import com.mjh.zhujianju.service.party.BuildingExamService;
import com.mjh.zhujianju.vo.party.BuildingExamResultVO;
import com.mjh.zhujianju.vo.party.BuildingExamTopicVO;
import com.mjh.zhujianju.vo.party.BuildingExamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Alex
 * @version: V1.0
 * @project: zhujianju
 * @package: com.mjh.zhujianju.controller.party
 * @description: description
 * @date: 2018/8/16
 * @time: 下午4:34
 */
@Controller()
@RequestMapping("party/exam")
public class ExamController extends BaseController<BuildingExamEO> {

    @Autowired
    private BuildingExamService buildingExamService;

    @Override
    protected BaseService<BuildingExamEO> getService() {
        return buildingExamService;
    }

    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getPage(BuildingExamVO buildingEventVO) {
        PagedResult<BuildingExamVO> list = buildingExamService.findBuildingExamPagination(buildingEventVO);
        return AjaxResult.getResult(list);
    }

	@RequestMapping(value = "getTopicPage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult getTopicPage(BuildingExamTopicVO buildingExamTopicVO) {
		PagedResult<BuildingExamTopicVO> list = buildingExamService.findBuildingExamTopicPagination(buildingExamTopicVO);
		return AjaxResult.getResult(list);
	}

	@RequestMapping(value="saveExamInfo",method=RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveExamInfo(BuildingExamVO buildingExamVO) {
    	Long id = null;
		if(buildingExamVO==null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误" );
		}
		try {
			id = buildingExamService.saveInfo(buildingExamVO);

		}catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}

		if(id != null){
			return AjaxResult.getResult(id);
		}
		return AjaxResult.getResult(Boolean.FALSE, "保存失败");
	}

	@RequestMapping(value = "editExam/{id}", method = RequestMethod.GET)
	public String editEvent(@PathVariable("id") Long id, Model model) {
		BuildingExamVO buildingExamVO = buildingExamService.queryBuildingExamInfo(id);
		model.addAttribute("buildingExam", buildingExamVO);
		return "party/exam/create";
	}

	@RequestMapping(value = "editExamTopic", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResult editEvent(Long id) {
		BuildingExamTopicVO buildingExamTopicVO = buildingExamService.queryBuildingExamTopicInfo(id);
		return AjaxResult.getResult(buildingExamTopicVO);
	}



	@RequestMapping(value = "delTopic", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult delTopic(Long id) {
		if (id == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			buildingExamService.delTopicByPrimaryKey(id);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "删除成功");
	}

	@RequestMapping(value="saveExamSubjects",method=RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveExamSubjects(@RequestBody BuildingExamTopicVO buildingExamTopicVO) {
		if(buildingExamTopicVO==null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误" );
		}
		try {
			buildingExamService.saveInfo(buildingExamTopicVO);

		}catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}

		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	@RequestMapping(value = "detailExam/{id}", method = RequestMethod.GET)
	public String detailExam(@PathVariable("id") Long id, Model model) {
		BuildingExamVO buildingExamVO = buildingExamService.findBuildingExamInfo(id);
		model.addAttribute("buildingExamVO", buildingExamVO);
		return "party/exam/detail1";
	}


	@RequestMapping(value = "detailUserResult/{id}", method = RequestMethod.GET)
	public String detailUserResult(@PathVariable("id") Long id, Model model) {
		return "party/exam/detail2";
	}

	@RequestMapping(value = "showResult", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult showResult(BuildingExamVO buildingExamVO) {
		BuildingExamVO buildingExamVO1 = buildingExamService.findBuildingExamResultInfo(buildingExamVO);
		return AjaxResult.getResult(buildingExamVO1);
	}


	@RequestMapping(value = "showPersonalResult/{id}/{examId}", method = RequestMethod.GET)
	public String showPersonalResult(@PathVariable("id") Long id, @PathVariable("examId") Long examId, Model model) {
    	try{
		    BuildingExamVO buildingExamVO = buildingExamService.findPersonalBuildingExamInfo(examId,id);
		    model.addAttribute("buildingExamVO", buildingExamVO);
	    }catch (XTBusinessException e){
		    model.addAttribute("buildingExamVO", null);
	    }

		model.addAttribute("id",id);
		model.addAttribute("examId",examId);
		return "party/exam/detail3";
	}

	@RequestMapping(value = "details/{id}/{mark}", method = RequestMethod.GET)
	public String details(@PathVariable("id") Long id, @PathVariable("mark") String mark,Model model) {
		model.addAttribute("id", id);
		model.addAttribute("mark", mark);
		return  "party/exam/detail";
	}

}
